//
//  ThreeTableViewCell.m
//  Music
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ThreeTableViewCell.h"

@implementation ThreeTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super  initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        //初始化数据源
        self.threedataSource = [NSMutableArray  array];
        
        self.flowLayout = [[UICollectionViewFlowLayout  alloc] init];
        self.flowLayout.itemSize = CGSizeMake((Screenwidth-26)/3, (Screenwidth-26)/3+20);
        self.flowLayout.scrollDirection =  UICollectionViewScrollDirectionVertical;
        //最小列间距
        self.flowLayout.minimumInteritemSpacing = 5 ;
        //最小行间距
        self.flowLayout.minimumLineSpacing = 8 ;
        //设置item据四边的间距
        self.flowLayout.sectionInset = UIEdgeInsetsMake(8 , 8 , 8 , 8);
        self.collectionView = [[UICollectionView  alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.bounces = NO;
        self.collectionView.backgroundColor = [UIColor  whiteColor];
        [self.collectionView  registerClass:[ThreeCollectionViewCell  class] forCellWithReuseIdentifier:@"three"];
        
        [self.contentView  addSubview:_collectionView];
    }
    
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    self.collectionView.frame =self.contentView.bounds;
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.threedataSource.count;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ThreeCollectionViewCell *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"three" forIndexPath:indexPath];
    
    cell.model = self.threedataSource[indexPath.row];
    
    
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    smallModel  *model = self.threedataSource[indexPath.row];
    NSString  *url = model.action[@"value"];
    NSInteger  number = [url  integerValue];
    [self.delegate  passNumber:number];
    
}
//-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//{
//    return CGSizeMake(Screenwidth, 30);
//}

//重写数组set方法:为了刷新数据:
-(void)setThreedataSource:(NSMutableArray *)threedataSource

{
    if (_threedataSource != threedataSource) {
        _threedataSource = threedataSource  ;
        [self.collectionView reloadData];
    }
   
}



- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
